'use strict';

module.exports = app => {
  const { model } = app;
  const { UUID, STRING, BOOLEAN } = app.Sequelize;

  const Role = model.define(
    'Role',
    {
      id: {
        type: UUID,
        primaryKey: true,
      },
      name: {
        type: STRING(50),
        allowNull: false,
      },
      identity: {
        type: STRING(50),
        allowNull: false,
      },
      description: {
        type: STRING(50),
        allowNull: false,
      },
      active: {
        type: BOOLEAN,
        defaultValue: true,
      },
    },
    {
      tableName: 'sys_role',
    }
  );

  Role.associate = function() {
    const { Sys: { User, Role, UserRole, Permission, RolePermission } } = model;
    Role.belongsToMany(User, { as: 'user', foreignKey: 'role_id', through: UserRole });
    Role.belongsToMany(Permission, { as: 'permissions', foreignKey: 'role_id', through: RolePermission });
  };

  return Role;
};
